package com.fwzc.giuseppe.fragment;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.activity.CommentActivity;
import com.fwzc.giuseppe.adapter.UniversalAdapter;
import com.fwzc.giuseppe.entity.Goods;
import com.fwzc.giuseppe.util.ViewHolder;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

@SuppressLint("InflateParams")
public class WaitCommentFragment extends BasicFragment implements OnRefreshListener{
	private ListView myListView;
	private UniversalAdapter<Goods> adapter;
	private List<Goods> datas = new ArrayList<Goods>();
	private SwipeRefreshLayout refreshLayout;
	private ProgressDialog load;
	private HttpUtils httpUtil;
	private BitmapUtils bitmapUtil;
	private String userId;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.wait_comment_fragment, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initView();
	}

	private void initView() {
		// 网络请求相关
		httpUtil = new HttpUtils();
		bitmapUtil = new BitmapUtils(getActivity());
		load = new ProgressDialog(getActivity());
		load.setMessage("数据加载中.....");
		load.setCancelable(false);
		userId = getActivity().getSharedPreferences("user", Activity.MODE_PRIVATE).getString(
				"userId", "");
		
		myListView = (ListView) getActivity().findViewById(
				R.id.wait_comment_list);
		refreshLayout = (SwipeRefreshLayout)getActivity().findViewById(R.id.refreshLayout_wait_comment);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setColorSchemeResources(android.R.color.holo_green_dark,android.R.color.holo_green_light,android.R.color.holo_orange_light,android.R.color.holo_red_light);
//		for (int i = 0; i < 6; i++) {
//			Goods order = new Goods();
//			order.goods_id = i;
//			order.goods_name = "这是乔治白最漂亮的衣服哦，赶紧买";
//			order.number = "X" + i;
//			order.price = "￥ 80" + i;
//			order.goods_code = "订单号:41545431212121";
//			datas.add(order);
//		}
		adapter = new UniversalAdapter<Goods>(getActivity(), datas,
				R.layout.comment_list_item) {
			@Override
			public void convert(ViewHolder viewHolder, final Goods order) {
				ImageView style = (ImageView) viewHolder.getView(R.id.clothes_image);
				TextView name = (TextView) viewHolder.getView(R.id.name);
				TextView code = (TextView) viewHolder.getView(R.id.order_code);
				TextView price = (TextView) viewHolder.getView(R.id.price);
				TextView number = (Button) viewHolder.getView(R.id.number);
				TextView comment = (Button) viewHolder.getView(R.id.comment);
				name.setText(order.goods_name);
				number.setText("x"+order.number);
				price.setText("￥"+order.price);
				code.setText("订单号:"+order.goods_code);
				BitmapDisplayConfig config = new BitmapDisplayConfig();
				config.setLoadFailedDrawable(getResources().getDrawable(R.drawable.error));
				bitmapUtil.display(style, order.goods_url, config);
				comment.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getActivity(),CommentActivity.class);
						intent.putExtra("suitId", order.goods_id);
						intent.putExtra("orderId", order.goods_code);
						getActivity().startActivity(intent);
					}
				});
			}
		};
		myListView.setAdapter(adapter);
		//快速滑动时停止图片的加载
		myListView.setOnScrollListener(new PauseOnScrollListener(bitmapUtil,false, true));
		sendRequest();
	}
	
	private void sendRequest() {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("userId", userId);
		params.addBodyParameter("type", "1");
		// 发送json类型的数据
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.ORDER_LIST, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						popNews("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 0) {
								JSONArray ja = response.getJSONArray("dataList");
								for (int i = 0; i < ja.length(); i++) {
									JSONObject jo = ja.getJSONObject(i);
									String orderNumber = jo.getString("orderNumber");
									JSONArray content = jo.getJSONArray("detailList");
									for (int j = 0; j < content.length(); j++) {
										JSONObject orderInfo = content.getJSONObject(j);
										Goods goods = new Goods();
										goods.goods_code = orderNumber;
										goods.goods_id = orderInfo.getInt("suitid");
										goods.goods_name = orderInfo.getString("name");
										goods.price = orderInfo.getString("price");
										goods.number = orderInfo.getString("amount");
										goods.goods_url = orderInfo.getString("imgurl");
										datas.add(goods);
									}
								}
								adapter.notifyDataSetChanged();
								if(refreshLayout.isRefreshing()){
									refreshLayout.setRefreshing(false);
								}
								load.dismiss();
							}else{
								load.dismiss();
								popNews(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							popNews("解析出错");
						}

					}
				});
	}

	
	@Override
	public void onRefresh() {
		refreshLayout.setRefreshing(true);
		datas.clear();
		sendRequest();
	}
}
